package com.sqfw.project.wp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.domain.WpOrganizerBack;
import com.sqfw.project.wp.service.IWpInfoService;
import com.sqfw.project.wp.service.WpAssistUnitService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 工单表Controller
 *
 * @author lx
 * @date 2023-06-07
 */
@Api(tags = "承办单位controller")
@RestController
@RequestMapping("/wp/assistUnitWp")
public class AssistUnitWpInfoController extends BaseController {

    @Autowired
    private IWpInfoService wpInfoService;

    @Autowired
    private WpAssistUnitService wpAssistUnitService;


    /**
     * 承办单位处理、领导处理
     * @param wpInfo
     * @return
     */
    @Log(title = "承办单位处理工单表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('organizerEdit')")
    @PostMapping("/organizerEdit")
    public AjaxResult organizerEdit(@RequestBody WpInfo wpInfo){

        return wpInfoService.updateWpInfo(wpInfo);
    }

    /**
     * 承办单位未处理工单列表/领导
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('organizerList')")
    @GetMapping("/organizerList")
    public TableDataInfo organizerList(WpInfo wpInfo) throws ParseException {
        //获取当前登录人信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptId = user.getDeptId();
        wpInfo.setDeptLevel2(deptId.toString());
        startPage();
        List<WpInfo> list = wpInfoService.organizerList(wpInfo);
        return getDataTable(list);
    }

    /**
     * 延期工单申请列表
     * @param wpInfo
     * @return
     * @throws ParseException
     */
    @PreAuthorize("@ss.hasPermi('getExtensionApplyList')")
    @PostMapping("/getExtensionApplyList")
    public TableDataInfo getExtensionApplyList(@RequestBody WpInfo wpInfo) {
        //获取当前登录人信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptId = user.getDeptId();
        wpInfo.setDeptLevel2(deptId.toString());
        startPage();
        List<WpInfo> list = wpInfoService.getExtensionApplyList(wpInfo);
        return getDataTable(list);
    }

    /**
     * 承办单位已处理工单列表(通过轨迹表查询)
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('organizerHandleList')")
    @GetMapping("/organizerHandleList")
    public TableDataInfo organizerHandleList(WpInfo wpInfo) throws ParseException {

        //获取当前登录人信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptId = user.getDeptId();
        wpInfo.setDeptLevel2(deptId.toString());

        startPage();
        TableDataInfo tableDataInfo = wpInfoService.selectprocessedWpInfoList(wpInfo);
        return tableDataInfo;
    }

    /**
     * 承办单位协助处理工单列表（查工单表）
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('assistHandleList')")
    @GetMapping("/assistHandleList")
    public TableDataInfo assistHandleList(WpInfo wpInfo) throws ParseException {

        //获取当前登录人信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptId = user.getDeptId();
        wpInfo.setDeptLevel2vice1(deptId.toString());
        wpInfo.setDeptLevel2vice2(deptId.toString());
        startPage();
        List<WpInfo> list = wpInfoService.selectWpInfoList(wpInfo);
        return getDataTable(list);
    }

    /**
     * 承办单位延期
     * @param wpInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('assistUnitWp:extensionEdit')")
    @PostMapping("/extensionEdit")
    public synchronized AjaxResult extensionEdit(@RequestBody WpInfo wpInfo){
        if ("0".equals(wpInfo.getExtensionStatus())){
            WpInfo info = wpInfoService.selectWpInfoByScid(wpInfo.getScid());
            if ("0".equals(info.getExtensionStatus())){
                return AjaxResult.error("已经申请延期，请等待领导审批！");
            }
        }
        return wpInfoService.extensionWpinfoEdit(wpInfo);
    }

    //-----------------------------2025-07-08:新加功能，承办单位退单，工单管理解锁----开始

    /**
     * 判断承办单位是否可以退单(非本单位处理件),超过三天，需要解；超过4天，不允许解锁
     * @param scid
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('assistUnitWp:isOrganizerBack')")
    @GetMapping("isOrganizerBack")
    public AjaxResult isOrganizerBack(@RequestParam("scid") String scid) {
        WpInfo wpInfo = new WpInfo();
        wpInfo.setScid(scid);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        wpInfo.setDeptLevel2(String.valueOf(user.getDeptId()));
        wpInfo.setNextState("承办单位处理");
        return wpAssistUnitService.isOrganizerBack(wpInfo);
    }

    /**
     * 承办单位退单
     * @param wpInfo
     * @return
     * */
    @Log(title = "承办单位退单", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('assistUnitWp:organizerBackWpInfo')")
    @PostMapping("organizerBackWpInfo")
    public AjaxResult organizerBackWpInfo(@RequestBody WpInfo wpInfo) {
        if (StringUtils.isBlank(wpInfo.getScid())) {
            return AjaxResult.error("工单id没有传递！");
        }
        if (StringUtils.isBlank(wpInfo.getState())) {
            return AjaxResult.error("工单当前状态没有传递！");
        }
        if (StringUtils.isBlank(wpInfo.getNextState())) {
            return AjaxResult.error("工单下一步状态没有传递");
        }
        if (StringUtils.isBlank(wpInfo.getLastmessage())) {
            return AjaxResult.error("工单退单描述没有传递！");
        }
        return wpAssistUnitService.organizerBackWpInfo(wpInfo);
    }

    /**
     * 判断工单管理页面是否允许解锁，承办单位超过3天到4天才能解锁
     * @param scid
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('assistUnitWp:isUntieOrganizerLock')")
    @GetMapping("isUntieOrganizerLock")
    public AjaxResult isUntieOrganizerLock(@RequestParam("scid") String scid) {
        WpInfo wpInfo = new WpInfo();
        wpInfo.setScid(scid);
        wpInfo.setNextState("承办单位处理");
        return wpAssistUnitService.isUntieOrganizerLock(wpInfo);
    }

    /**
     * 工单管理--工单解锁(解锁后，承办单位才能退单)
     * @param wpInfo
     * @return
     *  */
    @Log(title = "工单管理-工单解锁", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('assistUnitWp:untieOrganizerLock')")
    @PostMapping("untieOrganizerLock")
    public AjaxResult untieOrganizerLock(@RequestBody WpInfo wpInfo) {
        if (StringUtils.isBlank(wpInfo.getScid())) {
            return AjaxResult.error("工单id没有传递！");
        }
        wpInfo.setNextState("承办单位处理");
        return wpAssistUnitService.untieOrganizerLock(wpInfo);
    }

    /**
     * 查询承办单位退单的记录,和工单管理解锁记录
     * @param wpOrganizerBack
     * @return
     * */
    @PreAuthorize("@ss.hasPermi('assistUnitWp:pageWpOrganizerBack')")
    @GetMapping("pageWpOrganizerBack")
    public TableDataInfo pageWpOrganizerBack(WpOrganizerBack wpOrganizerBack) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Page page = new Page(pageNum, pageSize);
        Page<WpOrganizerBack> backPage = wpAssistUnitService.pageWpOrganizerBack(page, wpOrganizerBack);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(backPage.getRecords());
        rspData.setMsg("分页查询成功");
        rspData.setTotal(backPage.getTotal());
        return rspData;
    }

    //-----------------------------2025-07-08:新加功能，承办单位退单，工单管理解锁----结束









}
